package bod.engine.objs;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

import bod.engine.Engine;
import bod.engine.map.blocks.Block;

public abstract class Object2D {

	protected double			x;
	protected double			y;
	protected int				width;
	protected int				height;
	protected String			name;
	protected boolean			gravity;
	protected ArrayList<Vertex>	vertices;

	public Object2D(String name, int x, int y, int width, int height) {
		this.name = name;
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.gravity = true;
		this.vertices = new ArrayList<Vertex>();
	}

	public void hit() {
	}

	public void tick() {
		if (this.gravity) {
			if (this.inAir()) {
				this.move(0, Engine.getGravity());
			}
		}
	}

	public abstract BufferedImage getImage();

	protected boolean inAir() {
		boolean inAir = true;
		for (Vertex v : this.vertices) {
			int vx = this.getX() + v.getX();
			int vy = this.getY() + v.getY();
			Block b = Engine.getBlock(vx, vy);
			if (b != null && b.isSolid()) {
				inAir = false;
			}
		}
		return inAir;
	}

	public void move(double dx, double dy) {
		boolean left = dx < 0;
		boolean right = dx > 0;
		boolean up = dy < 0;
		boolean down = dy > 0;
		for (Vertex v : this.vertices) {
			int vx = this.getX() + v.getX();
			int vy = this.getY() + v.getY();
			if (left) {
				for (int x = vx; x > vx + dx; x--) {
					if (x == vx || x == vx + dx || x % Block.size == 0) {
						Block b = Engine.getBlock(x, vy);
						if (b != null && b.isSolid()) {
							int d = x - vx + 1;
							if (d > dx) {
								dx = d;
							}
						}
					}
				}
			} else if (right) {
				for (int x = vx; x < vx + dx; x++) {
					if (x == vx || x == vx + dx || x % Block.size == 0) {
						Block b = Engine.getBlock(x, vy);
						if (b != null && b.isSolid()) {
							int d = x - vx - 1;
							if (d < dx) {
								dx = d;
							}
						}
					}
				}
			}
			if (up) {
				for (int y = vy; y > vy + dy; y--) {
					if (y == vy || y == vy + dy || y % Block.size == 0) {
						Block b = Engine.getBlock(vx, y);
						if (b != null && b.isSolid()) {
							int d = y - vy + 1;
							if (d > dy) {
								dy = d;
							}
						}
					}
				}
			} else if (down) {
				for (int y = vy; y < vy + dy; y++) {
					Block b = Engine.getBlock(vx, y);
					if (y == vy || y == vy + dy || y % Block.size == 0) {
						if (b != null && b.isSolid()) {
							int d = y - vy - 1;
							if (d < dy) {
								dy = d;
							}
						}
					}
				}
			}
		}
		this.x += dx;
		this.y += dy;
	}

	public int getX() {
		return (int) this.x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return (int) this.y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return this.width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return this.height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public String getName() {
		return this.name;
	}

}